{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-11-17T11:32:49.420505Z",
     "start_time": "2024-11-17T11:32:49.417331Z"
    }
   },
   "source": [
    "import time\n",
    "import random\n",
    "import asyncio\n",
    "from uuid import uuid4"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-17T11:32:50.337608Z",
     "start_time": "2024-11-17T11:32:50.335371Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from dataclasses import dataclass\n",
    "\n",
    "\n",
    "@dataclass\n",
    "class Token:\n",
    "    uid: str\n",
    "    idx: int\n",
    "    cnt: int = 0"
   ],
   "id": "a6e4b0a4ce753871",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-17T11:32:54.761220Z",
     "start_time": "2024-11-17T11:32:54.758854Z"
    }
   },
   "cell_type": "code",
   "source": [
    "async def connect_web(data):\n",
    "    t = random.randint(0, 2)\n",
    "    await asyncio.sleep(t)  # 使用 asyncio.sleep 替代 time.sleep\n",
    "    return data * 10"
   ],
   "id": "606288e344ca1246",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-17T11:32:56.466847Z",
     "start_time": "2024-11-17T11:32:56.464727Z"
    }
   },
   "cell_type": "code",
   "source": [
    "async def call_api(token, data):\n",
    "    token.cnt += 1\n",
    "    return await connect_web(data)"
   ],
   "id": "489c4760163057e2",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-17T11:32:58.094885Z",
     "start_time": "2024-11-17T11:32:58.091588Z"
    }
   },
   "cell_type": "code",
   "source": [
    "workders = 25\n",
    "tokens = [Token(uid=str(uuid4()), idx=i) for i in range(workders)]\n",
    "\n",
    "\n",
    "async def main():\n",
    "    nums = 100\n",
    "    data = [i for i in range(nums)]\n",
    "    result = [call_api(tokens[int(i % workders)], item) for i, item in enumerate(data)]\n",
    "    # 使用 asyncio.gather 调用异步任务\n",
    "    results = await asyncio.gather(*result)\n",
    "    return results"
   ],
   "id": "9e845fc4cc9706dc",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-17T11:33:01.964148Z",
     "start_time": "2024-11-17T11:32:59.945892Z"
    }
   },
   "cell_type": "code",
   "source": "result = await main()",
   "id": "ad8496ff8b401536",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-17T11:33:06.428515Z",
     "start_time": "2024-11-17T11:33:06.423461Z"
    }
   },
   "cell_type": "code",
   "source": "result",
   "id": "8993c6b35dca72fb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 10,\n",
       " 20,\n",
       " 30,\n",
       " 40,\n",
       " 50,\n",
       " 60,\n",
       " 70,\n",
       " 80,\n",
       " 90,\n",
       " 100,\n",
       " 110,\n",
       " 120,\n",
       " 130,\n",
       " 140,\n",
       " 150,\n",
       " 160,\n",
       " 170,\n",
       " 180,\n",
       " 190,\n",
       " 200,\n",
       " 210,\n",
       " 220,\n",
       " 230,\n",
       " 240,\n",
       " 250,\n",
       " 260,\n",
       " 270,\n",
       " 280,\n",
       " 290,\n",
       " 300,\n",
       " 310,\n",
       " 320,\n",
       " 330,\n",
       " 340,\n",
       " 350,\n",
       " 360,\n",
       " 370,\n",
       " 380,\n",
       " 390,\n",
       " 400,\n",
       " 410,\n",
       " 420,\n",
       " 430,\n",
       " 440,\n",
       " 450,\n",
       " 460,\n",
       " 470,\n",
       " 480,\n",
       " 490,\n",
       " 500,\n",
       " 510,\n",
       " 520,\n",
       " 530,\n",
       " 540,\n",
       " 550,\n",
       " 560,\n",
       " 570,\n",
       " 580,\n",
       " 590,\n",
       " 600,\n",
       " 610,\n",
       " 620,\n",
       " 630,\n",
       " 640,\n",
       " 650,\n",
       " 660,\n",
       " 670,\n",
       " 680,\n",
       " 690,\n",
       " 700,\n",
       " 710,\n",
       " 720,\n",
       " 730,\n",
       " 740,\n",
       " 750,\n",
       " 760,\n",
       " 770,\n",
       " 780,\n",
       " 790,\n",
       " 800,\n",
       " 810,\n",
       " 820,\n",
       " 830,\n",
       " 840,\n",
       " 850,\n",
       " 860,\n",
       " 870,\n",
       " 880,\n",
       " 890,\n",
       " 900,\n",
       " 910,\n",
       " 920,\n",
       " 930,\n",
       " 940,\n",
       " 950,\n",
       " 960,\n",
       " 970,\n",
       " 980,\n",
       " 990]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-17T11:33:24.687545Z",
     "start_time": "2024-11-17T11:33:24.684655Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for token in tokens:\n",
    "    print(token)"
   ],
   "id": "e936950faf71c77e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Token(uid='38efc42f-df20-4eff-bf89-9d83be72f754', idx=0, cnt=4)\n",
      "Token(uid='979838a0-1dd6-4312-addf-26678891f129', idx=1, cnt=4)\n",
      "Token(uid='7d735d9a-a088-42ca-8b69-5b8f0de527ac', idx=2, cnt=4)\n",
      "Token(uid='eb77fb0f-bae5-48c6-99d7-cfc455421529', idx=3, cnt=4)\n",
      "Token(uid='68285f4e-9da9-4c32-8939-f6143fd67160', idx=4, cnt=4)\n",
      "Token(uid='56539cbb-b142-429e-ac5b-3fed19c8cbfd', idx=5, cnt=4)\n",
      "Token(uid='42129219-1441-44be-b106-74cb2bc03151', idx=6, cnt=4)\n",
      "Token(uid='ec5cdd15-c62e-476a-ab91-db64e53236e1', idx=7, cnt=4)\n",
      "Token(uid='51fb99c0-573f-4e1a-9ade-42897730d868', idx=8, cnt=4)\n",
      "Token(uid='6faae40a-63b1-4d9f-8a6d-29804201f544', idx=9, cnt=4)\n",
      "Token(uid='6221c9b4-f330-43d5-a708-3d3fc143ce28', idx=10, cnt=4)\n",
      "Token(uid='a1ec68c5-7435-44bc-a31b-b7bf101fbbed', idx=11, cnt=4)\n",
      "Token(uid='b6744836-fd61-4c8f-9a26-85345e01fd16', idx=12, cnt=4)\n",
      "Token(uid='4b21f380-e2ed-45b3-824b-ce7402ecc0bd', idx=13, cnt=4)\n",
      "Token(uid='58a3d23f-4a64-4753-8e2f-91b8269deba6', idx=14, cnt=4)\n",
      "Token(uid='afc5e886-cff6-4fb6-9d25-f0dae5b0a87f', idx=15, cnt=4)\n",
      "Token(uid='cfcd0c54-3b00-4df4-b008-7b5146294b90', idx=16, cnt=4)\n",
      "Token(uid='a6412f3e-d850-41ad-aefc-fda58e12b747', idx=17, cnt=4)\n",
      "Token(uid='1c91abb6-807b-47ea-b8ec-59a4326fb992', idx=18, cnt=4)\n",
      "Token(uid='73453fab-d190-4833-863a-37ecd667efb1', idx=19, cnt=4)\n",
      "Token(uid='695946a1-557d-41ee-8cc5-08c408d33775', idx=20, cnt=4)\n",
      "Token(uid='e0b72b88-5d2c-43f7-958a-12ef8fac7e5b', idx=21, cnt=4)\n",
      "Token(uid='f055dd12-c31e-4ad1-a987-a094fa64fc9e', idx=22, cnt=4)\n",
      "Token(uid='3f843e8e-4473-4be3-b7af-6618a86313c4', idx=23, cnt=4)\n",
      "Token(uid='75ece1e7-3173-4a4c-8acf-b486e45be3b0', idx=24, cnt=4)\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "81b2d72199a3b822"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "44123efbd5c9870"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
